﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class GenericFieldValidator
    {
        public static bool IsValid(List<SPDynamicFieldValidationRule> Rules, SPDynamicField Field)
        {
            //if (IsRequired && Value.ToString().Trim().Length == 0)
            //{
            //    LastError = "is required";
            //    return false;
            //}

            if (Rules.Count == 0)
                return true;
            else
            {
                foreach (SPDynamicFieldValidationRule item in Rules)
                {
                    switch (item.ValidationType)
                    {
                        //ToDo:Compare Validaor for eq,gt,lt with a field
                        case "re":
                            item.IsValid = Validation.StringValidator.IsBlank(item, Field);
                            break;
                        case "xl":
                            item.IsValid = Validation.StringValidator.MaxLength(item, Field);
                            break;
                        case "ml":
                            item.IsValid = Validation.StringValidator.MinLength(item, Field);
                            break;
                        case "fl":
                            item.IsValid = Validation.StringValidator.FixedLength(item, Field);
                            break;
                        case ">":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.GreaterThan(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.NumericValidator.GreaterThan(item, Field);
                            }
                            break;
                        case ">=":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.GreaterThanEqual(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.NumericValidator.GreaterThanEqual(item, Field);
                            }
                            break;
                        case "<":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.LessThan(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.NumericValidator.LessThan(item, Field);
                            }
                            break;
                        case "<=":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.LessThanEqual(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.NumericValidator.LessThanEqual(item, Field);
                            }
                            break;
                        case "=":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.Equal(item, Field);
                            }
                            else if (Field.FieldType == "Number" || Field.FieldType == "Integer" || Field.FieldType == "Currency")
                            {
                                item.IsValid = Validation.NumericValidator.Equal(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.StringValidator.Equal(item, Field);
                            }
                            break;
                        case "!=":
                            if (Field.FieldType == "DateTime")
                            {
                                item.IsValid = Validation.DateValidator.NotEqual(item, Field);
                            }
                            else if (Field.FieldType == "Number" || Field.FieldType == "Integer" || Field.FieldType == "Currency")
                            {
                                item.IsValid = Validation.NumericValidator.NotEqual(item, Field);
                            }
                            else
                            {
                                item.IsValid = Validation.StringValidator.NotEqual(item, Field);
                            }
                            break;
                        case "=>":
                            item.IsValid = Validation.RangeValidator.Between(item, Field);
                            break;
                        case "==":
                            item.IsValid = Validation.CompareValidator.Compare(item, Field);
                            break;
                        case "rx":
                            item.IsValid = Validation.StringValidator.RegExMatch(item, Field);
                            break;
                        case "()":
                            item.IsValid = Validation.StringValidator.Contains(item, Field);
                            break;
                        case ")(":
                            item.IsValid = Validation.StringValidator.DoesNotContains(item, Field);
                            break;
                        //default:
                        //    return true;

                    }

                    if (!item.IsValid)
                    {
                        Field.LastError = item.ValidationMessage;
                        return false;
                    }
                }
                return true;
            }
        }
    }
}
